<template>
    <view>
        <view @tap="xzfl" class="xzfl" :style="'background-color:' + color">+新增分类</view>
        <form @reset="formReset" @submit="formSubmit1">
            <view class="weui-cells weui-cells_after-title" v-if="isxz">
                <view class="weui-cell weui-cell_input">
                    <view class="weui-cell__hd">
                        <view class="weui-label">分类名称</view>
                    </view>
                    <view class="weui-cell__bd">
                        <input class="weui-input" name="flmc" placeholder="请输入分类名称" value="" />
                    </view>
                </view>
                <view class="weui-cell weui-cell_input">
                    <view class="weui-cell__hd">
                        <view class="weui-label">排序序号</view>
                    </view>
                    <view class="weui-cell__bd">
                        <input class="weui-input" name="pxxh" placeholder="数字越小越靠前" type="number" value="" />
                    </view>
                </view>
                <view class="weui-cell weui-cell_switch">
                    <view class="weui-cell__bd">是否显示</view>
                    <view class="weui-cell__ft">
                        <switch checked :color="color" name="sfxs"></switch>
                    </view>
                </view>
            </view>
            <view class="weui-btn-area" v-if="isxz">
                <button @tap="qx" class="weui-btn" style="background-color: #e5e5e5; color: #999" type="primary">取消</button>
                <button class="weui-btn" :disabled="disabled1" formType="submit" :style="'background-color:' + color" type="primary">提交</button>
            </view>
        </form>
        <view class="weui-cells weui-cells_after-title">
            <view class="weui-cell bottom headtitle">
                <view class="weui-cell__hd hdwidth">ID</view>
                <view class="weui-cell__bd">分类名称</view>
                <view class="weui-cell__bd bdwidth">状态</view>
                <view class="weui-cell__ft">
                    <view style="margin-left: 40rpx">查看</view>
                    <view style="margin-left: 40rpx">操作</view>
                </view>
            </view>
            <form @reset="formReset" @submit="formSubmit" v-for="(item, index) in flarr" :key="item.id">
                <view class="weui-cell bottom">
                    <view class="weui-cell__hd hdwidth">{{ item.id }}</view>
                    <view class="weui-cell__bd">{{ item.type_name }}</view>
                    <view class="weui-cell__bd bdwidth" :style="'color:' + color" v-if="item.is_open == '1'">显示</view>
                    <view class="weui-cell__bd bdwidth" v-else>隐藏</view>
                    <view class="weui-cell__ft">
                        <image @tap="bianji" :data-id="item.id" src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/edit.png"></image>
                        <image @tap="sc" :data-id="item.id" src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/delete.png"></image>
                    </view>
                </view>

                <view class="bottom" v-if="item.id == activeid">
                    <view class="weui-cell weui-cell_input">
                        <view class="weui-cell__hd">
                            <view class="weui-label">分类名称</view>
                        </view>
                        <view class="weui-cell__bd">
                            <input class="weui-input" name="flmc" placeholder="请输入分类名称" :value="item.type_name" />
                        </view>
                    </view>
                    <view class="weui-cell weui-cell_input">
                        <view class="weui-cell__hd">
                            <view class="weui-label">排序序号</view>
                        </view>
                        <view class="weui-cell__bd">
                            <input class="weui-input" name="pxxh" placeholder="数字越小越靠前" type="number" :value="item.order_by" />
                        </view>
                    </view>
                    <view class="weui-cell weui-cell_switch">
                        <view class="weui-cell__bd">是否显示</view>
                        <view class="weui-cell__ft">
                            <switch :checked="item.is_open == '1'" :color="color" name="sfxs"></switch>
                        </view>
                    </view>
                    <view class="weui-btn-area">
                        <button @tap="qxbj" class="weui-btn" style="background-color: #e5e5e5; color: #999" type="primary">取消</button>
                        <button class="weui-btn" formType="submit" :style="'background-color:' + color" type="primary">确定提交</button>
                    </view>
                </view>
            </form>
        </view>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var app = getApp();
var util = require('../../../utils/util.js');
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            activeid: '',
            disabled: false,
            disabled1: false,
            isxz: false,
            flarr: '',
            color: ''
        };
    },
    onLoad: function (t) {
        app.globalData.setNavigationBarColor(this);
        this.reLoad();
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    methods: {
        qxbj: function () {
            this.setData({
                activeid: ''
            });
        },

        xzfl: function () {
            this.setData({
                isxz: true,
                activeid: ''
            });
        },

        qx: function () {
            this.setData({
                isxz: false
            });
        },

        bianji: function (t) {
            var e = t.currentTarget.dataset.id;
            console.log(e);
            this.setData({
                activeid: e
            });
        },

        sc: function (t) {
            var that = this;
            var a = t.currentTarget.dataset.id;
            console.log(a);
            uni.showModal({
                title: '提示',
                content: '确认删除此分类吗？',
                success: function (t) {
                    if (t.confirm) {
                        console.log('用户点击确定');
                        app.globalData.util.request({
                            url: 'entry/wxapp/DelGoodsType',
                            cachetime: '0',
                            data: {
                                id: a
                            },
                            success: function (t) {
                                console.log(t);
                                if (1 == t.data) {
                                    uni.showToast({
                                        title: '操作成功',
                                        duration: 1000
                                    });
                                    setTimeout(function () {
                                        that.reLoad();
                                    }, 1000);
                                }
                            }
                        });
                    } else {
                        if (t.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                }
            });
        },

        reLoad: function () {
            var that = this;
            var t = uni.getStorageSync('sjdsjid');
            console.log(t);
            app.globalData.util.request({
                url: 'entry/wxapp/GoodsType',
                cachetime: '0',
                data: {
                    store_id: t
                },
                success: function (t) {
                    console.log(t.data);
                    that.setData({
                        flarr: t.data
                    });
                }
            });
        },

        formSubmit1: function (t) {
            console.log('form1发生了submit事件，携带数据为：', t.detail.value);
            var that = this;
            if (t.detail.value.sfxs) {
                var a = 1;
            } else {
                a = 2;
            }
            var o = uni.getStorageSync('sjdsjid');
            var i = t.detail.value.flmc;
            var s = t.detail.value.pxxh;
            console.log('', o, i, s, a);
            var n = '';
            var l = true;
            if ('' == i) {
                n = '请填写商品名称！';
            } else {
                if ('' == s) {
                    n = '请填写排序序号！';
                } else {
                    that.setData({
                        disabled1: true
                    });
                    l = false;
                    app.globalData.util.request({
                        url: 'entry/wxapp/UpdGoodsType',
                        cachetime: '0',
                        data: {
                            order_by: s,
                            type_name: i,
                            is_open: a,
                            store_id: o,
                            id: ''
                        },
                        success: function (t) {
                            console.log(t);
                            if (1 == t.data) {
                                uni.showToast({
                                    title: '操作成功'
                                });
                                setTimeout(function () {
                                    that.reLoad();
                                    that.setData({
                                        isxz: false,
                                        disabled1: false
                                    });
                                }, 1000);
                            } else {
                                that.setData({
                                    disabled1: false
                                });
                                uni.showToast({
                                    title: '请修改后提交！',
                                    icon: 'loading'
                                });
                            }
                        }
                    });
                }
            }
            if (1 == l) {
                uni.showModal({
                    title: '提示',
                    content: n
                });
            }
        },

        formSubmit: function (t) {
            console.log('form发生了submit事件，携带数据为：', t.detail.value);
            var that = this;
            var a = this.activeid;
            if (t.detail.value.sfxs) {
                var o = 1;
            } else {
                o = 2;
            }
            var i = uni.getStorageSync('sjdsjid');
            var s = t.detail.value.flmc;
            var n = t.detail.value.pxxh;
            console.log(a, i, s, n, o);
            var l = '';
            var d = true;
            if ('' == s) {
                l = '请填写商品名称！';
            } else {
                if ('' == n) {
                    l = '请填写排序序号！';
                } else {
                    that.setData({
                        disabled: true
                    });
                    d = false;
                    app.globalData.util.request({
                        url: 'entry/wxapp/UpdGoodsType',
                        cachetime: '0',
                        data: {
                            order_by: n,
                            type_name: s,
                            is_open: o,
                            store_id: i,
                            id: a
                        },
                        success: function (t) {
                            console.log(t);
                            if (1 == t.data) {
                                uni.showToast({
                                    title: '操作成功'
                                });
                                setTimeout(function () {
                                    that.reLoad();
                                    that.setData({
                                        activeid: '',
                                        disabled: false
                                    });
                                }, 1000);
                            } else {
                                that.setData({
                                    disabled: false
                                });
                                uni.showToast({
                                    title: '请修改后提交！',
                                    icon: 'loading'
                                });
                            }
                        }
                    });
                }
            }
            if (1 == d) {
                uni.showModal({
                    title: '提示',
                    content: l
                });
            }
        },

        formReset() {
            console.log('占位：函数 formReset 未声明');
        }
    }
};
</script>
<style>
@import './spfl.css';
</style>
